package cn.wolfcode.trip.app.controller;

import cn.wolfcode.trip.base.domain.Lottery;
import cn.wolfcode.trip.base.domain.ShopingCar;
import cn.wolfcode.trip.base.domain.Travel;
import cn.wolfcode.trip.base.domain.User;
import cn.wolfcode.trip.base.query.OrderQueryObject;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.query.ShopingCarQueryObject;
import cn.wolfcode.trip.base.query.TravelQueryObject;
import cn.wolfcode.trip.base.service.*;
import cn.wolfcode.trip.base.util.JSONResult;
import cn.wolfcode.trip.base.util.UserContext;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * 用户APP资源控制器
 */
@RestController
@RequestMapping("users")
public class UserController {
    @Autowired
    private IUserService userService;
    @Autowired
    private ITravelService travelService;
    @Autowired
    private IPointService pointService;

    @Autowired
    private IShopingCarService shopingCarService;
    @Autowired
    private IOrderService orderService;
    @Autowired
    private ILotteryService lotteryService;
    /**
     * 用户注册资源接口
     * @param user
     * @return
     */
    @PostMapping
    public JSONResult register(User user){
        JSONResult result = new JSONResult();
        try {
            userService.saveOrUpdate(user);
        } catch (Exception e) {
            result.mark(e.getMessage());
            e.printStackTrace();
        }
        return result;
    }
    //修改更新 是put请求,使用@PutMapping
    @PutMapping("{id}")
    public JSONResult update(User user){
        JSONResult jsonResult = new JSONResult();
        userService.saveOrUpdate(user);
        jsonResult.setResult(user);
        return jsonResult;
    }

    /**
     * 根据作者的id查游记
     * @param qo
     * @return
     */
    @GetMapping("{authorId}/travels")
    public PageInfo queryTravel(TravelQueryObject qo){
        qo.setState(Travel.STATE_RELEASE);
        qo.setOrderBy("t.lastUpdateTime desc");
        return travelService.query(qo);
    }

    /**
     * 查询用户的相关信息
     * @param authorId
     * @return
     */
    @GetMapping("{authorId}")
    public User getUser(@PathVariable Long authorId){
        return userService.get(authorId);
    }
    /**
     * 查询用户的购物车
     */
    @GetMapping("{userId}/products")
    public PageInfo queryProduct(ShopingCarQueryObject qo){
        PageInfo pageInfo = userService.queryProducts(qo);
        return pageInfo;
    }

//    @PostMapping("checkEmail")
//    public boolean checkEmail(String email){
//        return userService.checkEmail(email);
//    }

    /**
     * 关注别人
     * @param theirsId：要关注的用户的id
     * @return
     */
    @PostMapping("{theirsId}/follows")
    public JSONResult follows(@PathVariable Long theirsId){
        userService.follows(theirsId);
        return new JSONResult();
    }

    /**
     * 取消关注
     * @param theirsId：对谁取消关注
     * @return
     */
    @DeleteMapping("{theirsId}/cancelFollow")
    public JSONResult cancelFollow(@PathVariable Long theirsId){
        userService.cancelFollow(theirsId);
        return new JSONResult();
    }

    /**
     * 查询某个用户的粉丝集合
     * @param id：用户id
     * @return
     */
    @GetMapping("{id}/fans")
    public List<User> getFans(@PathVariable Long id){
       List<Long>  fansIds = userService.getFansByUserId(id);
       List<User> list = new ArrayList<>();
       for (Long fansId : fansIds) {
            list.add(userService.get(fansId));
        }
        return list;
    }

    /**
     * 查询某个用户关注了哪些人
     * @param id：用户id
     * @return
     */
    @GetMapping("{id}/follow")
    public List<User> getFollow(@PathVariable Long id){
        List<Long>  followIds = userService.getFollowByUserId(id);
        List<User> list = new ArrayList<>();
        for (Long followId : followIds) {
            list.add(userService.get(followId));
        }
        return list;
    }
    @GetMapping("{theirsId}/hasFollowed")
    public Boolean hasFollowed(@PathVariable Long theirsId){
        System.out.println("------");
        List<Long>  followIds = userService.getFollowByUserId(UserContext.getCurrentUser().getId());
        System.out.println(followIds);
        if(followIds.contains(theirsId)){
            return true;
        }
        return false;
    }
    /**
     * 删除用户的 某一指定商品
     * @param productId
     * @return
     */
    @DeleteMapping("{productId}")
    public JSONResult delete(@PathVariable Long productId){
        shopingCarService.delete(productId);
        return new JSONResult();
    };

    /**
     * 用户购物车产品数量增减操作
     */
    @PostMapping("{user.id}")
    public JSONResult updateNum(ShopingCar shopingCar){
        shopingCarService.updateNum(shopingCar);
        return new JSONResult();
    }
    /**
     * 查询用户的积分
     */
    @GetMapping("{id}/integrals")
    public Integer getIntegral(@PathVariable Long id){
        Integer integer = pointService.getIntegral(id);
        return integer;
    }

    /**
     * 订单的新增
     */
    @PostMapping("{userId}/orders")
    public JSONResult saveOrder(@PathVariable Long userId,Long[] productIds,Integer[] numbers,int difference){
        orderService.saveOrder(userId,productIds,numbers,difference);
        return new JSONResult();
    }

    /**
     * 更新用户积分
     * @param
     * @return
     */
    @PostMapping("{userId}/integrals")
    public JSONResult updateIntegral(@PathVariable Long userId,Integer integral){
        pointService.updateIntegralByUserId(userId,integral);
        return new JSONResult();
    }
    /**
     * 高级查询用户的订单
     */
    @GetMapping("{userId}/orders")
    public PageInfo queryOrders(OrderQueryObject qo){
        qo.setOrderBy("o.createTime desc");
        PageInfo pageInfo = orderService.queryOrders(qo);
        return pageInfo;
    }

    @GetMapping("{id}/lotteries")
    public Integer getLotteries(@PathVariable Long id){
        return lotteryService.getLottery(id);
    }
    @PostMapping("{id}/lotteries")
    public JSONResult updateLottery(@PathVariable Long id,Integer time){
        Lottery lottery = new Lottery();
        lottery.setId(id);
        lottery.setTime(time);
        lotteryService.update(lottery);
        return new JSONResult();
    }


}
